package org.xtext.ic.dsl.generator;

import com.google.inject.Inject;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.ComparableExtensions;
import org.eclipse.xtext.xbase.lib.IntegerExtensions;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.eclipse.xtext.xtend2.lib.ResourceExtensions;
import org.eclipse.xtext.xtend2.lib.StringConcatenation;
import org.xtext.ic.dsl.generator.ModelExtensions;
import org.xtext.ic.dsl.iCDsl.Entity;
import org.xtext.ic.dsl.iCDsl.Model;
import org.xtext.ic.dsl.iCDsl.PackagePrefix;

@SuppressWarnings("all")
public class ICDslGenerator implements IGenerator {
  
  @Inject
  private IQualifiedNameProvider nameProvider;
  
  @Inject
  private ModelExtensions models;
  
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
    {
      EList<EObject> _contents = resource.getContents();
      EObject _head = IterableExtensions.<EObject>head(_contents);
      Model m = ((Model) _head);
      PackagePrefix _package = m.getPackage();
      PackagePrefix pp = _package;
      Iterable<EObject> _allContentsIterable = ResourceExtensions.allContentsIterable(resource);
      Iterable<Entity> _filter = IterableExtensions.<Entity>filter(_allContentsIterable, org.xtext.ic.dsl.iCDsl.Entity.class);
      for (final Entity e : _filter) {
        {
          QualifiedName _fullyQualifiedName = this.nameProvider.getFullyQualifiedName(e);
          List<String> _segments = _fullyQualifiedName.getSegments();
          List<String> layerFileNames = _segments;
          String entityFileName = "";
          int i = 1;
          boolean _operator_notEquals = ObjectExtensions.operator_notEquals(pp, null);
          if (_operator_notEquals) {
            {
              String _name = pp.getName();
              String[] _split = _name.split("\\.");
              String[] prefixFileNames = _split;
              int j = 1;
              for (final String fileName : prefixFileNames) {
                {
                  String _operator_plus = StringExtensions.operator_plus(entityFileName, fileName);
                  String _operator_plus_1 = StringExtensions.operator_plus(_operator_plus, "/");
                  entityFileName = _operator_plus_1;
                  int _operator_plus_2 = IntegerExtensions.operator_plus(((Integer)j), ((Integer)1));
                  j = _operator_plus_2;
                }
              }
            }
          }
          for (final String fileName_1 : layerFileNames) {
            {
              int _size = layerFileNames.size();
              boolean _operator_lessThan = ComparableExtensions.<Integer>operator_lessThan(((Integer)i), ((Integer)_size));
              if (_operator_lessThan) {
                String _operator_plus_3 = StringExtensions.operator_plus(entityFileName, fileName_1);
                String _operator_plus_4 = StringExtensions.operator_plus(_operator_plus_3, "/");
                entityFileName = _operator_plus_4;
              } else {
                String _operator_plus_5 = StringExtensions.operator_plus(entityFileName, fileName_1);
                String _operator_plus_6 = StringExtensions.operator_plus(_operator_plus_5, ".java");
                entityFileName = _operator_plus_6;
              }
              int _operator_plus_7 = IntegerExtensions.operator_plus(((Integer)i), ((Integer)1));
              i = _operator_plus_7;
            }
          }
          StringConcatenation _compile = this.models.compile(m, e);
          fsa.generateFile(entityFileName, _compile);
        }
      }
    }
  }
}